<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary" size="mini" @click="Addtheclassification"
          >添加分类</el-button
        >
      </div>
      <!-- 文章表格 -->
      <el-table border stripe :data="CateList" style="width: 100%">
        <el-table-column type="index" label="序号" width="120">
        </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="mini" type="primary" @click="Modify(scope.row)"
              >修改</el-button
            >
            <el-button size="mini" type="danger" @click="odd(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加文章分类弹框 -->
    <el-dialog
      @closed="addCloseHandler"
      title="添加文章分类"
      :visible.sync="dialogVisible"
      width="35%"
      :before-close="handleClose"
    >
      <!--  -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="ruleForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="ruleForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <!--  -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="add" size="mini">添加</el-button>
      </span>
    </el-dialog>

    <!-- 修改文章分类 -->
    <el-dialog
      title="修改文章分类"
      :visible.sync="edthVisible"
      width="35%"
      :before-close="handleClose"
    >
      <!--  -->
      <el-form
        :model="edthFrom"
        :rules="rules"
        ref="edthForm"
        label-width="70px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cate_name">
          <el-input v-model="edthFrom.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
          <el-input v-model="edthFrom.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <!--  -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="edthVisibless" size="mini">取 消</el-button>
        <el-button type="primary" @click="edth" size="mini">却认</el-button>
      </span>
    </el-dialog>
    <!--  -->
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      CateList: [],
      dialogVisible: false,
      // 修改
      edthVisible: false,
      edthFrom: {
        cate_name: '',
        cate_alias: ''
      },
      ruleForm: {
        cate_name: '',
        cate_alias: ''
      },
      rules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          {
            pattern: /^\S{1,10}$/,
            message: '分类名必须是1-10位的非空字符',
            trigger: 'blur'
          }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,15}$/,
            message: '分类别名必须是1-15位的字母数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getArtCate()
  },
  methods: {
    // 添加文章分类
    add () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.dialogVisible = false
          this.$http
            .post('/my/cate/add', this.ruleForm)
            .then(({ data: res }) => {
              if (res.code === 0) {
                this.$message({
                  message: res.message,
                  type: 'success'
                })
                // 重新获取列表
                this.getArtCate()
                // 失败逻辑
              } else {
                this.$message.error(res.message)
              }
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 关闭对话框
    Addtheclassification () {
      this.dialogVisible = true
    },
    // 获取文章分类
    async getArtCate () {
      const { data: res } = await this.$http.get('/my/cate/list')
      //  获取成功赋值给data里面的数据
      if (res.code === 0) {
        this.CateList = res.data
      }
    },
    // 清空表单
    addCloseHandler () {
      this.$refs.ruleForm.resetFields()
    },
    // 弹窗是否却忍关闭
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    // 点击修改弹出弹框
    Modify (row) {
      // 如果点击前面两个管理员禁止修改
      if (row.id === 1 || row.id === 2) {
        this.$message.error('管理员禁止修改')
        return
      }
      this.edthVisible = true

      // 数据拿到页面
      this.edthFrom = { ...row }
    },
    // 却认修改
    edth () {
      this.$refs.edthForm.validate((valid) => {
        if (valid) {
          this.$http
            .put('/my/cate/info', this.edthFrom)
            .then(({ data: res }) => {
              // 如果修改成功关闭对话框
              if (res.code === 0) {
                this.edthVisible = false
                // 提示用户
                this.$message({
                  message: res.message,
                  type: 'success'
                })
                // 刷新页面
                this.getArtCate()
              }
            })
          this.edthVisible = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 取消时候为false
    edthVisibless () {
      this.edthVisible = false
    },
    // 删除文章分类
    odd (id) {
      if (id === 1 || id === 2) {
        return this.$message.error('管理员禁止删除')
      }
      this.$confirm('是否确认删除此文章分类?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        // 却认删除
        .then(() => {
          // 发请求查询是params,直接写是默认data
          this.$http
            .delete('/my/cate/del', { params: { id } })
            .then(({ data: res }) => {
              if (res.code === 0) {
                // 删除成功
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                // 再次从服务器拿数据刷新页面
                this.getArtCate()
              }
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
